class Solution
{
public:
    vector<int> inventoryManagement(vector<int>& stock, int cnt)
    {
        srand(time(NULL));
        qsort(stock, 0, stock.size() - 1, cnt);
        return { stock.begin(), stock.begin() + cnt };
    }

    void qsort(vector<int>& stock, int l, int r, int cnt)
    {
        if (l >= r) return;

        int key = getRandom(stock, l, r);
        int left = l - 1, right = r + 1, i = l;
        while (i < right)
        {
            if (stock[i] < key) swap(stock[++left], stock[i++]);
            else if (stock[i] == key) i++;
            else swap(stock[--right], stock[i]);
        }

        int a = left - l + 1, b = right - left - 1;
        if (a > cnt) qsort(stock, l, left, cnt);
        else if (a + b >= cnt) return;
        else qsort(stock, right, r, cnt - a - b);
    }

    int getRandom(vector<int>& stock, int l, int r)
    {
        return stock[rand() % (r - l + 1) + l];
    }
};